package cn.biplam.front.component.data.mdManage;

import cn.biplam.common.ajxcode.MdCode.BmObjectSelectCode;
import cn.biplam.common.ajxcode.MdCode.BmTableMaintainCode;
import cn.biplam.common.ajxcode.MdCode.DwObjectSelectCode;
import cn.biplam.common.ajxcode.MdCode.DwTableMaintainCode;
import cn.biplam.common.ajxcode.MdCode.SrcObjectSelectCode;
import cn.biplam.common.ajxcode.MdCode.SrcTableMaintainCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.mdManage.TypeCode.OptModelIndexCode;
import cn.biplam.common.paramters.ModelType;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.paramters.MdParam.Index_Node_Move_Type;
import cn.biplam.common.paramters.ModelType.Model_Object_Type;
import cn.biplam.common.paramters.ModelType.Model_Type;
import cn.biplam.common.struc.BaseStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.tree.OptTreeRecordId;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Align_Type;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.htmlscript.script.AjxScript.Is_SubMitData;
import cn.biplam.front.template.base.RadioGroup;
import cn.biplam.front.template.base.Text;
import cn.biplam.front.template.base.Radio.Layout_Type;
import cn.biplam.front.template.control.FlatTable;

/**
 * 元数据管理—索引表移动类型选择<br>
 * 在索引表节点移动中<br>
 * 当选择完被移动的对象和移动的目标对象后 选择移动类型
 * @author XiaoRuxing 2009-6-17下午06:05:59
 * @version 1.0
 */
public class MdObjectMoveTypeSelect extends AjxResponse{

	/** 索引表移动类型选择组件ID */
	public static final String cId="1013";
	/** 获取索引表移动类型 */
	public static final String Get_Move_Type="101301";

	private String srcType;
	private String srcName;
	private String targType;
	private String targName;

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(actionId.equals(Get_Move_Type)){
			return get();
		}else{
			throw new ErrorMessage("Ajx Exchange outport not found:"+ actionId+" !");
		}
	}

	public HtmlWindow get() throws ErrorMessage{
		HtmlWindow div=new HtmlDiv(cId);
		String treeIndex = params.getByKey(TreeParam.TREE_INDEX);
		OptTreeRecordId record = new OptTreeRecordId(treeIndex);
		HtmlValue hiddenKey=new HtmlValue(cId+"r",cId+"r",treeIndex);//被移动节点信息
		div.put(hiddenKey);
		String targetTableID= recordId;
		HtmlValue moveToTableID=new HtmlValue("h,"+cId+"movec","h,"+cId+"movec",targetTableID);//移动目标节点ID
		div.put(moveToTableID);
		String modelType = record.getIndexNodeType();

		if(record.getRealID().equals(targetTableID)){
			_generatorException(div, "不能移动到自己!", modelType);
			return div;
		}

		if(TOOLS.StringIsEmpty(targetTableID)){
			_generatorException(div, "请先选择目标对象!", modelType);
			return div;
		}

		_getSrcData(record.getRealID(), modelType);
		_getTargData(targetTableID, modelType);
		div.put(this._drawTitle());
		
		if(_getIsParent(record.getRealID(), targetTableID, modelType)){
			_generatorException(div, "不能移动到子节点!", modelType);
			return div;
		}

		if(this.targType.equals(Model_Object_Type.ROOT.toString())){
			_generatorException(div, "根主题不能作为目标!", modelType);
			return div;
		}
		
		if(this.srcType.equals(Model_Object_Type.NON_ROOT.toString())&&
				this.targType.equals(Model_Object_Type.TABLE.toString())){
			_generatorException(div, "表不能作为主题移动的目标!", modelType);
			return div;
		}

		_getMoveTypeSelect(div, modelType);
		return div;
	}
	
	/**
	 * 获取允许移动情况下的移动类型选择界面
	 */
	private void _getMoveTypeSelect(HtmlWindow div,String modelType) throws ErrorMessage{
		
		//获取提交脚本
		String script = _submitScript(modelType);
		
		//选择表移动类型
		RadioGroup moveType=new RadioGroup(cId+"type",sHomeUrl,Layout_Type.Horizontal);
		//表移动到主题的 儿子
		//表移动到表的 兄弟
		//主题移动到主题的 兄弟或者儿子
		//如果是表移动到表,设置移动类型有：移动到前面移动到后面,移动到后面为默认类型
		if((Model_Object_Type.TABLE.toString().equals(this.targType)&&
				Model_Object_Type.TABLE.toString().equals(this.srcType))){
			moveType.set(Index_Node_Move_Type.Move_After.toString(), "移动到后面",true);
			moveType.set(Index_Node_Move_Type.Move_Before.toString(), "移动到前面");
			
			//在弹出窗口选择完移动类型后提交
			div.bind(SysName.PromptSubmitID, Window_Event.OnClick, script);
		}else if(Model_Object_Type.NON_ROOT.toString().equals(this.targType)&&
				Model_Object_Type.TABLE.toString().equals(this.srcType)){//如果是表移动到主题,设置移动类型有：移动到儿子
			moveType.set(Index_Node_Move_Type.Move_Child_End.toString(), "移动到下级",true);
			
			//默认移动到下级不弹出窗口直接提交
			div.bind(script);
		}else{//如果是主题移动到主题,设置移动类型有：儿子或者兄弟，移动到儿子为默认
			moveType.set(Index_Node_Move_Type.Move_Child_End.toString(), "移动到下级",true);
			moveType.set(Index_Node_Move_Type.Move_After.toString(), "移动到后面");
			moveType.set(Index_Node_Move_Type.Move_Before.toString(), "移动到前面");
			
			//在弹出窗口选择完移动类型后提交
			div.bind(SysName.PromptSubmitID, Window_Event.OnClick, script);
		}
		
		HtmlWindow move = moveType.get();
		move.setMarginTop("10");
		move.setStyleHeight("25");
		move.setStyleHeight("10");
		div.put(move);
		div.setStyleHeight("100");
	}
	
	/**
	 *  根据当前树类型生成移动操作提交脚本
	 */
	private String _submitScript(String modelType) throws ErrorMessage{
		String script = null;
		if(modelType.equals(Model_Type.Src.toString())){//根据传入的树节点类型判断是属于那个业务模型的移动
			//移动操作提交脚本
			script = AjxScript.getSpNoRefresh(cId,  
					SrcTableMaintainCode.Table_Move_Opt);
			script = PopupScript.prSubmit(script);
		}else if(modelType.equals(Model_Type.Bm.toString())){
			//移动操作提交脚本
			script = AjxScript.getSpNoRefresh(cId,  
					BmTableMaintainCode.Table_Move_Opt);
			script = PopupScript.prSubmit(script);
		}else if(modelType.equals(Model_Type.Dw.toString())){
			//移动操作提交脚本
			script = AjxScript.getSpNoRefresh(cId,  
					DwTableMaintainCode.Table_Move_Opt);
			script = PopupScript.prSubmit(script);
		}
		return script;
	}
	
	/**
	 * 获取被移动节点信息
	 */
	private void _getSrcData(String nodeID,String modelType) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_CODE, nodeID);
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE, modelType);
		OperatorResult or =	Operator.get(OptModelIndexCode.GetBy_RealId_And_ModelType, mUser.getUserID(), filter);
		if(or == null || or.isNull()){
			throw new ErrorMessage("Can not get src node!");
		}
		this.srcType = or.getValue(BaseStruc.IndexTableStruc.OPERATOR_TYPE);
		this.srcName = or.getValue(BaseStruc.IndexTableStruc.NODE_NAME);
	}

	/**
	 * 获取目标节点信息
	 */
	private void _getTargData(String nodeID,String modelType) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_CODE, nodeID);
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE, modelType);
		OperatorResult or =	Operator.get(OptModelIndexCode.GetBy_RealId_And_ModelType, mUser.getUserID(), filter);
		if(or == null || or.isNull()){
			throw new ErrorMessage("Can not get src node!");
		}
		this.targType = or.getValue(BaseStruc.IndexTableStruc.OPERATOR_TYPE);
		this.targName = or.getValue(BaseStruc.IndexTableStruc.NODE_NAME);
	}

	/**
	 * 绘制标题界面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _drawTitle() throws ErrorMessage{
		FlatTable table = new FlatTable(cId+"t",recordId);
		table.putTh("被移动对象", 0, 0);
		table.putTd(srcName, 0, 1);
		table.putTd(ModelType.getModelObjectTypeName(this.srcType), 0, 2);
		table.putTh("目标对象", 1, 0);
		table.putTd(targName, 1, 1);
		table.putTd(ModelType.getModelObjectTypeName(this.targType), 1, 2);
		table.setColSize(0, 10);
		return table.get();
	}
	
	/**
	 * 生成异常情况下的界面
	 */
	private void _generatorException(HtmlWindow div ,String title,String modelType) throws ErrorMessage{
		HtmlWindow expTxt = new Text(title).get();
		expTxt.setStyleWidth("100",Length_Unit.percent);
		expTxt.setTextAlign(Align_Type.center);
		expTxt.setMarginTop("10");
		expTxt.setFontColor("red");
		expTxt.setFontSize("18");
		div.put(expTxt);
		div.setStyleHeight("100");
		_getExceptionScript(modelType, div);
	}

	/**
	 * 获取异常情况下的确定按钮脚本
	 * @param modelType
	 * @param div
	 * @throws ErrorMessage
	 */
	private void _getExceptionScript(String modelType,HtmlWindow div) throws ErrorMessage{
		String script = null;
		if(modelType.equals(Model_Type.Src.toString())){
			script = AjxScript.getPr(cId, SrcObjectSelectCode.Get_Src_Move_Tree, Is_SubMitData.Submit);
			div.bind(SysName.PromptSubmitID, Window_Event.OnClick, script);
		}else if(modelType.equals(Model_Type.Bm.toString())){
			script = AjxScript.getPr(cId, BmObjectSelectCode.Get_Bm_Move_Tree, Is_SubMitData.Submit);
			div.bind(SysName.PromptSubmitID, Window_Event.OnClick, script);
		}else if(modelType.equals(Model_Type.Dw.toString())){
			script = AjxScript.getPr(cId, DwObjectSelectCode.Get_Dw_Move_Tree, Is_SubMitData.Submit);
			div.bind(SysName.PromptSubmitID, Window_Event.OnClick, script);
		}
	}

	/**
	 * 判断被移动节点是否是移动到目标节点的父亲，如果是则不能移动 
	 */
	private boolean _getIsParent(String srcID,String targetID,String modelType) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_CODE, targetID);
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE, modelType);
		filter.put(BaseStruc.IndexTableStruc.NODE_PARENT, srcID);
		OperatorResult rs = Operator.get(OptModelIndexCode.Query_Is_Parent, mUser.getUserID(), filter);
		String isParent = rs.getValue();
		if("1".equals(isParent)){
			return true;
		}else{
			return false;
		}
	}

}
